package com.gyms.activity;

import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;

import com.classic.okhttp.enums.Enum;
import com.classic.okhttp.services.IUserService;
import com.classic.okhttp.zmoumall.http.ObjectCallback;
import com.classic.okhttp.zmoumall.utils.MD5;
import com.classic.okhttp.zmoumall.utils.ToastUtil;
import com.gyms.R;
import com.gyms.base.MyAutoLayoutActivity;
import com.gyms.manager.DataCenter;
import com.gyms.manager.TitleManager;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.OnClick;
import constant.EventConstant;
import utils.StringUtils;
import weight.TimeButton;
import weight.UiEvent;

/**
 * 找回密码 yjl
 */
public class FindPassActivity extends MyAutoLayoutActivity {
    @BindView(R.id.et_phone)
    EditText etPhone;
    @BindView(R.id.et_code)
    EditText etCode;
    @BindView(R.id.btn_getcode)
    TimeButton btnGetcode;
    @BindView(R.id.et_newpass)
    EditText etNewpass;
    @BindView(R.id.et_newpass_again)
    EditText etNewpassAgain;

    private String phone;
    private String code;
    private String passWord,conFimPass;
    @Override
    protected void setTitleTheme() {
        mTheme = TitleManager.TitleTheme.White;
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_find_pass;
    }

    @Override
    protected void initView() {
        setTitle("找回密码");
        createLoadingView("请求中",true);
        mTitleManager.setRightText("确定");
        setBack();
        phone= DataCenter.getInstance().getUser().getPhone();
    }

    @Override
    protected void setListener() {
        mTitleManager.getmRightTV().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (verification()) {
                    if (loadingView != null) {
                        loadingView.show();
                    }
                    String pass= MD5.MD5Encode(new StringBuffer(conFimPass).toString());
                    IUserService.findPassword(FindPassActivity.this, phone, pass, code, false, new ObjectCallback<Object>() {
                        @Override
                        public void onSuccess(Object response, String message) {
                            dialogDis();
                            ToastUtil.showToast(mContext,"修改密码成功");
                            EventBus.getDefault().post(new UiEvent(EventConstant.FLAG_FIND_PASS_SUC,null));
                            finish();
                        }

                        @Override
                        public void onErrorT(int code, Object response, String errorMessage) {
                            dialogDis();
                            ToastUtil.showToast(mContext,errorMessage);
                        }

                        @Override
                        public void onHttpErrorCall(int statusCode) {
                            dialogDis();
                        }
                    });
                }
            }
        });
    }

    @Override
    protected void processLogic() {

    }

    @OnClick(R.id.btn_getcode)
    public void onClick() {
        //请求签证码
//        phone = etPhone.getText().toString().trim();
//        if (!RegexUtils.isMobileSimple(phone)) {
//            ToastUtils.showShortToast(mContext, "请输入正确的手机号");
//            btnGetcode.setStart(false);
//            return;
//        }
        reQuestVerify();
    }

    /**
     * 请求验证码接口
     */
    private void reQuestVerify() {
        loadingView.show();
        IUserService.getVerify(this, phone, Enum.EnumVerifyType.Findpassword ,false, new ObjectCallback<Object>() {
            @Override
            public void onSuccess(Object response, String message) {
                dialogDis();
                ToastUtil.showToast(mContext,message);
                btnGetcode.setStartOrNot(true);

            }

            @Override
            public void onErrorT(int code, Object response, String errorMessage) {
                dialogDis();
                ToastUtil.showToast(mContext,errorMessage);
            }

            @Override
            public void onHttpErrorCall(int statusCode) {
                dialogDis();
            }
        });
    }

    /**
     * 表单验证
     */
    private boolean verification() {
        passWord=etNewpass.getText().toString().trim();
        conFimPass=etNewpassAgain.getText().toString().trim();
//        phone = etPhone.getText().toString().trim();
        code = etCode.getText().toString().trim();
        if (StringUtils.isEmpty(code)) {
            ToastUtil.showToast(mContext, "请输入验证码");
            return false;
        }else if(StringUtils.isEmpty(passWord)||StringUtils.isEmpty(conFimPass)){
            ToastUtil.showToast(mContext,"请输入密码");
            return false;
        }else if(!passWord.equals(conFimPass)){
            ToastUtil.showToast(mContext,"两次密码不一致");
            return false;
        }else {
            return true;
        }
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        hideKeyBoard(ev);//隐藏软键盘
        return super.dispatchTouchEvent(ev);
    }
}
